package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.PageRequestDto;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.wemedia.dtos.WmChannelDto;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.pojos.WmChannel;
import com.heima.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.mapper.WmChannelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 郭文杰
 * @description 针对表【wm_channel(频道信息表)】的数据库操作Service实现
 * @createDate 2023-12-04 10:08:42
 */
@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel>
        implements WmChannelService {

    @Autowired
    private WmChannelMapper wmChannelMapper;

    @Autowired
    private WmNewsMapper wmNewsMapper;

    /**
     * admin端,频道列表分页查询
     *
     * @param pageRequestDto 请求参数 页码 每页显示条数
     * @return 当前页列表数据
     */
    @Override
    public List<WmChannel> channelPage(PageRequestDto pageRequestDto) {
        //pageHelper和分页插件的分页查询不能混着用会报错,这里改成了单独使用分页插件
        /*if (pageRequestDto == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Page<WmChannel> page = PageHelper.startPage(pageRequestDto.getPage(), pageRequestDto.getSize());
        wmChannelMapper.channelPage(pageRequestDto);
        return page.getResult();*/
        IPage<WmChannel> page = new Page<>(pageRequestDto.getPage(), pageRequestDto.getSize());
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(pageRequestDto.getName() != null && !(pageRequestDto.getName().equals("")),
                        WmChannel::getName, pageRequestDto.getName())
                 .eq(pageRequestDto.getStatus() != null, WmChannel::getStatus, pageRequestDto.getStatus())
                .orderByDesc(WmChannel::getCreatedTime);
        wmChannelMapper.selectPage(page, wrapper);
        return page.getRecords();
    }

    /**
     * 新增频道
     *
     * @param wmChannelDto 前端参数
     */
    @Override
    public void saveChannel(WmChannelDto wmChannelDto) {
        //校验参数
        if (wmChannelDto.getDescription() == null || wmChannelDto.getDescription().equals("") ||
                wmChannelDto.getName() == null || wmChannelDto.getName().equals("") ||
                wmChannelDto.getOrd() == null ||
                wmChannelDto.getStatus() == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断当前频道是否存在
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmChannel::getName, wmChannelDto.getName());
        WmChannel wmChannel = wmChannelMapper.selectOne(wrapper);
        if (wmChannel != null) {
            throw new CustomException(AppHttpCodeEnum.CHANNEL_INSERT_ERROR);
        }
        wmChannel = new WmChannel();
        wmChannel.setDescription(wmChannelDto.getDescription());
        wmChannel.setName(wmChannelDto.getName());
        wmChannel.setOrd(wmChannelDto.getOrd());
        wmChannel.setStatus(wmChannelDto.getStatus() ? 1 : 0);
        wmChannel.setIsDefault(1);
        wmChannel.setCreatedTime(new Date());
        wmChannelMapper.insert(wmChannel);
    }

    /**
     * 修改频道
     *
     * @param wmChannelDto 前端参数
     */
    @Override
    public void updateChannel(WmChannelDto wmChannelDto) {
        //校验参数
        if (wmChannelDto.getId() == null ||
                wmChannelDto.getName() == null || wmChannelDto.getName().equals("") ||
                wmChannelDto.getStatus() == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //  查询当前频道是否被引用
        //如果频道id在自媒体中被引用,不能禁用,并抛出异常
        LambdaQueryWrapper<WmNews> wmNewsWrapper = new LambdaQueryWrapper<>();
        wmNewsWrapper.eq(wmChannelDto.getId() != null, WmNews::getChannelId, wmChannelDto.getId());
        List<WmNews> wmNews = wmNewsMapper.selectList(wmNewsWrapper);
        if (wmNews.size() != 0) {
            throw new CustomException(AppHttpCodeEnum.CHANNEL_STOP_ERROR);
        }
        //没有被引用,则可以禁用当前频道
        Integer status = wmChannelDto.getStatus() ? 1 : 0;
        // update wm_channel set status = ... where id = ...
        LambdaUpdateWrapper<WmChannel> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(wmChannelDto.getId() != null, WmChannel::getId, wmChannelDto.getId())
                .set(wmChannelDto.getStatus() != null, WmChannel::getStatus, status);
        this.update(updateWrapper);
    }

    /**
     * 删除频道
     *
     * @param id 频道id
     */
    @Override
    public void delChannel(Integer id) {
        //参数校验
        if (id == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //查询该频道是否被禁用
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmChannel::getId, id);
        WmChannel wmChannel = wmChannelMapper.selectOne(wrapper);
        if (wmChannel.getStatus() == 1) {
            throw new CustomException(AppHttpCodeEnum.CHANNEL_DELETE_ERROR);
        }
        wmChannelMapper.deleteById(id);
    }


}




